﻿using System.Collections.Generic;
using System.Web.Mvc;
using FogBugzPd.Core;
using FogBugzPd.Core.FbAccountModule;
using FogBugzPd.Core.FogBugzApi;
using FogBugzPd.Infrastructure;
using FogBugzPd.Web.Models.FbAccount;
using FogBugzPd.Application;
using System.Linq;
using FogBugzPd.Infrastructure.Web;
using FogLampz.Model;

namespace FogBugzPd.Web.Controllers
{
    public class FbAccountController : ControllerBase
    {
		[HttpGet]
		[CustomAuthorize]
		public ActionResult Settings()
		{
			var fbAccount = DbContext.FbAccounts
			                          .Include("Settings")
			                          .Include("Settings.TestRailConfig")
			                          .Single(a => a.Id == UserContext.FbAccountId);
			FbAccountSettings fbAccountSettings;

			if (fbAccount == null)
				fbAccountSettings = new FbAccountSettings();
			else
				fbAccountSettings = fbAccount.Settings;

			var viewModel = new SettingViewModel();
	
			viewModel.Setup();

			MapSettingsViewModel(viewModel, fbAccountSettings);

			return View("Update", viewModel);
        }

	    [HttpPost]
		[CustomAuthorize]
		public ActionResult Settings(SettingViewModel viewModel)
	    {
			viewModel.Setup();

			if (!ModelState.IsValid)
			{
				return View("Update", viewModel);
			}

			var fbAccount = DbContext.FbAccounts
									.Include("Settings")
									.Include("Settings.TestRailConfig")
									.SingleOrDefault(s => s.Id == viewModel.Id);

			FbAccountSettings fbAccountSettings;

			if (fbAccount == null)
				fbAccountSettings = new FbAccountSettings();
			else
				fbAccountSettings = fbAccount.Settings;

			//refresh cache when qa estimate settings changed
			if (fbAccountSettings.AllowQaEstimates != viewModel.AllowQaEstimates
				|| fbAccountSettings.QaEstimateCustomFieldname != viewModel.QaEstimateCustomFieldname
				|| fbAccountSettings.QaPercentage != viewModel.QaPercentage)
		    {
				CleareCachedCaseSets();
		    }

			MapSettingsEntity(fbAccountSettings, viewModel);

		    var service = new FbAccountService();

			// save settings in db
			service.Update(fbAccount);

			UserContext.SetNotification(PageNotificationType.Success, "Settings have been updated.");

			return RedirectToAction("Settings");
	    }

	    private void MapSettingsViewModel(SettingViewModel viewModel, FbAccountSettings fbAccountSettings)
	    {
			viewModel.Id = fbAccountSettings.Id;
		    viewModel.ResolvedVerifiedStatusId = fbAccountSettings.ResolvedVerifiedStatusId;
			viewModel.AllowSubProjects = fbAccountSettings.AllowSubProjects;
			viewModel.QaPercentage = fbAccountSettings.QaPercentage;
			viewModel.AllowTestRail = fbAccountSettings.AllowTestRail;

			if (fbAccountSettings.TestRailConfig != null)
			{
				viewModel.TestRailUrl = fbAccountSettings.TestRailConfig.Url;
				viewModel.TestRailToken = fbAccountSettings.TestRailConfig.Token;
			}
		    
			viewModel.SubProjectTag = fbAccountSettings.SubProjectTag;

		    viewModel.AllowQaEstimates = fbAccountSettings.AllowQaEstimates;
		    viewModel.QaEstimateCustomFieldname = fbAccountSettings.QaEstimateCustomFieldname;
	    }

		private void MapSettingsEntity(FbAccountSettings entity, SettingViewModel viewModel)
		{
			entity.Id = viewModel.Id;
			entity.ResolvedVerifiedStatusId = viewModel.ResolvedVerifiedStatusId;
			entity.AllowSubProjects = viewModel.AllowSubProjects;
			entity.QaPercentage = viewModel.QaPercentage ?? 0;
			entity.AllowTestRail = viewModel.AllowTestRail;

			if (entity.AllowTestRail)
			{
				if(entity.TestRailConfig == null)
					entity.TestRailConfig = new TestRailConfiguration();


				entity.TestRailConfig.Url = viewModel.TestRailUrl;
				entity.TestRailConfig.Token = viewModel.TestRailToken;
			}
			entity.SubProjectTag = viewModel.SubProjectTag;

			entity.AllowQaEstimates = viewModel.AllowQaEstimates;
			entity.QaEstimateCustomFieldname = viewModel.QaEstimateCustomFieldname;
		}

	    private void CleareCachedCaseSets()
	    {
			IList<FixFor> milestones = FogBugzGateway.GetMilestones();

		    milestones.ToList().ForEach(
			    milestone =>
				    {
					    string cacheKey = MsCacheKey.GenCaseSetKey(milestone.IndexProject.Value, milestone.Index.Value);

					    MsCache.TryRemove(cacheKey);

				    }
			    );


			IList<Case> subProjectParentCases = FogBugzGateway.GetSubProjectParentCases();

		    if (subProjectParentCases != null)
			    subProjectParentCases.ToList().ForEach(
				    parentCase =>
					    {
						    string cacheKey = MsCacheKey.GenCaseSetKey(parentCase.Project.Index.Value, parentCase.IndexFixFor.Value,
						                                               parentCase.Index);
						    MsCache.TryRemove(cacheKey);
					    }
				    );

	    }
    }
}
